package com.hmeter.hadoop.mapreduce.jobtracker;

import java.io.IOException;

import org.apache.jmeter.samplers.AbstractSampler;
import org.apache.jmeter.samplers.Entry;
import org.apache.jmeter.samplers.SampleResult;

public class JobTrackerSampler extends AbstractSampler {

	/**
	 * 
	 */
	private static final long serialVersionUID = -682285548981168704L;
	
	private static final String JOBTRACKER = "JOBTRACKER";
	private static final String JOBID = "JOBID";
	private static final String GROUPNAME = "GROUPNAME";
	private static final String JOBTRACKERMETRICBOOL = "JOBTRACKERMETRICBOOL";
	private static final String JOBCOUNTERSMETRICBOOL = "JOBCOUNTERSMETRICBOOL";
	private static final String JOBSTATUSMETRICBOOL = "JOBSTATUSMETRICBOOL";
	private static final String TASKCOUNTERSMETRICBOOL = "TASKCOUNTERSMETRICBOOL";
	private static final String TASKSTATUSMETRICBOOL = "TASKSTATUSMETRICBOOL";
	
	@Override
	public SampleResult sample(Entry e) {
			SampleResult result = new SampleResult();
		
		String requestData = "job tracker: " + this.getJobTracker() + "\n";
		requestData += "job id: " + this.getJobId() + "\n";
		requestData += "group name: " + this.getGroupName() + "\n";
		
		result.setSampleLabel(getName());
		result.setSamplerData(requestData);
		result.setDataType(SampleResult.TEXT);

		result.sampleStart();
		
		if (this.isJobTrackerMetricBool())
		{
			JobTrackerMetrics jt = new JobTrackerMetrics();
			try {
				String response = jt.getJobTrackerMetric(this.getJobTracker());
				result.setResponseData(response.getBytes());
				result.setSuccessful(true);
			} catch (IOException e1) {
				result.setResponseData(e1.getMessage().getBytes());
				result.setSuccessful(false);
			}
		}
		
		if (this.isJobCountersMetricBool())
		{
			JobCountersMetric jc = new JobCountersMetric();
			try {
				String response = jc.getJobCountersMetric(this.getJobTracker(), this.getJobId(), this.getGroupName());
				result.setResponseData(response.getBytes());
				result.setSuccessful(true);
			} catch (IOException e1) {
				result.setResponseData(e1.getMessage().getBytes());
				result.setSuccessful(false);
			}
		}
		
		if (this.isJobStatusMetricBool())
		{
			JobCountersMetric jc = new JobCountersMetric();
			try {
				String response = jc.getJobStatusMetric(this.getJobTracker(), this.getJobId());
				result.setResponseData(response.getBytes());
				result.setSuccessful(true);
			} catch (IOException e1) {
				result.setResponseData(e1.getMessage().getBytes());
				result.setSuccessful(false);
			}
		}
		
		if (this.isTaskCountersMetricBool())
		{
			TaskCountersMetric tc = new TaskCountersMetric();
			try {
				String response = tc.getTaskCountersMetric(this.getJobTracker(), this.getJobId(), this.getGroupName());
				result.setResponseData(response.getBytes());
				result.setSuccessful(true);
			} catch (IOException e1) {
				result.setResponseData(e1.getMessage().getBytes());
				result.setSuccessful(false);
			}
		}
		
		if (this.isTaskStatusMetricBool())
		{
			TaskCountersMetric tc = new TaskCountersMetric();
			try {
				String response = tc.getTaskStatusMetric(this.getJobTracker(), this.getJobId());
				result.setResponseData(response.getBytes());
				result.setSuccessful(true);
			} catch (IOException e1) {
				result.setResponseData(e1.getMessage().getBytes());
				result.setSuccessful(false);
			}
		}
		
		result.sampleEnd();
		return result;
	}
	
	public void setJobTracker(String jobTracker)
	{
		setProperty(JOBTRACKER, jobTracker);
	}
	
	public String getJobTracker()
	{
		return getPropertyAsString(JOBTRACKER);
	}
	
	public void setJobId(String jobId)
	{
		setProperty(JOBID, jobId);
	}
	
	public String getJobId()
	{
		return getPropertyAsString(JOBID);
	}
	
	public void setGroupName(String groupName)
	{
		setProperty(GROUPNAME, groupName);
	}
	
	public String getGroupName()
	{
		return getPropertyAsString(GROUPNAME);
	}
	
	public boolean isJobTrackerMetricBool()
	{
		return getPropertyAsBoolean(JOBTRACKERMETRICBOOL);
	}
	
	public void setJobTrackerMetricBool(boolean jobTrackerMetric)
	{
		setProperty(JOBTRACKERMETRICBOOL, jobTrackerMetric);
	}
	
	public boolean isJobCountersMetricBool()
	{
		return getPropertyAsBoolean(JOBCOUNTERSMETRICBOOL);
	}
	
	public void setJobCountersMetricBool(boolean jobCountersMetric)
	{
		setProperty(JOBCOUNTERSMETRICBOOL, jobCountersMetric);
	}
	
	public boolean isJobStatusMetricBool()
	{
		return getPropertyAsBoolean(JOBSTATUSMETRICBOOL);
	}
	
	public void setJobStatusMetricBool(boolean jobStatusMetric)
	{
		setProperty(JOBSTATUSMETRICBOOL, jobStatusMetric);
	}
	
	public boolean isTaskCountersMetricBool()
	{
		return getPropertyAsBoolean(TASKCOUNTERSMETRICBOOL);
	}
	
	public void setTaskCountersMetricBool(boolean taskCountersMetric)
	{
		setProperty(TASKCOUNTERSMETRICBOOL, taskCountersMetric);
	}
	
	public boolean isTaskStatusMetricBool()
	{
		return getPropertyAsBoolean(TASKSTATUSMETRICBOOL);
	}
	
	public void setTaskStatusMetricBool(boolean taskStatusMetric)
	{
		setProperty(TASKSTATUSMETRICBOOL, taskStatusMetric);
	}

}
